//
//  AskQuestionsController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/17.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "AskQuestionsController.h"
#import "ReleaseTextAndImageController.h"
#import "AskQuestionsSuccessfulVC.h"

@interface AskQuestionsController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**标题输入框*/
@property (nonatomic,strong)PubulicTextView*topTextView;
/**描述输入框*/
@property (nonatomic,strong)PubulicTextView*bottomTextView;
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;

/**导航栏左边文字按钮*/
@property (nonatomic,strong)UIButton * rightBtn;

@end

@implementation AskQuestionsController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavItems];
    [self createUI];

    if (self.type == AskQuestionsAnswer) {
        self.topTextView.textView.text = @"西红柿炒蛋先放西红柿还是先放鸡蛋？";
        self.topTextView.textView.userInteractionEnabled = NO;
        self.topTextView.placeholderText.hidden = YES;
        
        self.bottomTextView.placeholderText.text = @"填写回答内容~";
        
        [self.rightBtn setTitle:@"发布" forState:UIControlStateNormal];
    }
}
-(void)releseAction{
    NSMutableArray * images = [NSMutableArray array];//图片地址数组
    for (NSInteger i = 0 ; i < self.dataArray.count ; i++) {
        PhotoModel * model = self.dataArray[i];
        NSString * name = [ImageAndVideoSaveTool get_ONLYID];
        [images addObject:name];
        [ImageAndVideoSaveTool saveImageDocuments:model.highDefinitionImage imageName:name];
    }
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:@"购物狂" forKey:@"name"];
    [dic setValue:@"user2" forKey:@"userIcon"];
    [dic setValue:self.topTextView.textView.text forKey:@"title"];
    [dic setValue:images forKey:@"images"];
    if (self.bottomTextView.textView.text.length>0) {
        [dic setValue:self.bottomTextView.textView.text forKey:@"describe"];
    }
    [PlistTool savePlistValue:dic Type:ReleseQanswer];
    [self postNotification];
}

-(void)postNotification{
    [[NSNotificationCenter defaultCenter] postNotificationName:K_ReleaseQuestions object:nil];
}
-(void)setNavItems{
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 50, 30);
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitleColor:UIColorFromRGB(0x273323) forState:UIControlStateNormal];
    leftBtn.titleLabel.font = SYSTEMFONT(16);
    
    self.rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rightBtn setTitle:@"提问" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
    self.rightBtn.titleLabel.font = SYSTEMFONT(16);
    self.rightBtn.frame = CGRectMake(0, 0, 50, 30);
    kWeakSelf(weakSelf);
    [self.rightBtn addActionHandler:^{
        if (!StrValid(self.topTextView.textView.text)) {
            ShowHubMessage(@"请输入您的问题");
            return;
        }
        [weakSelf releseAction];
        if (weakSelf.type != AskQuestionsAnswer) {
            [weakSelf.navigationController pushViewController:[AskQuestionsSuccessfulVC new] animated:YES];
        }else{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }
    }];
    
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
    
    
}
-(void)createUI{
    [self.view addSubview:self.topTextView];
    [self.view addSubview:self.bottomTextView];
    [self.view addSubview:self.listView];
    
    [self.topTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(40);
    }];
    [self.bottomTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(self.topTextView.mas_bottom).offset(20);
        make.right.mas_equalTo(-16);
        make.height.mas_equalTo(40);
    }];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(16);
        make.top.mas_equalTo(self.bottomTextView.mas_bottom).offset(20);
        make.right.mas_equalTo(-16);
        make.bottom.mas_equalTo(0);
    }];
}
#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count + 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ReleaseImageItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(ReleaseImageItem.class) forIndexPath:indexPath];
    if (indexPath.row == self.dataArray.count) {
        cell.isAdd = YES;
    }else{
        cell.model = self.dataArray[indexPath.row];
        cell.isAdd = NO;
        kWeakSelf(weakSelf);
        cell.deleBlck = ^{
            DLogFunc;
            [weakSelf.dataArray removeObjectAtIndex:indexPath.row];
            [weakSelf.listView reloadData];
        };

    }
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == self.dataArray.count ) {
        kWeakSelf(weakSelf);
//        [PhotosManager showPhotosManager:self withMaxImageCount:10 withAlbumArray:^(NSMutableArray<PhotoModel *> *albumArray) {
//            [weakSelf.dataArray addObjectsFromArray:albumArray];
//            [weakSelf.listView reloadData];
//        }];
        [PhotosManager showPhotosManager:self withMaxImageCount:9 withAlbumArray:^(NSMutableArray<PhotoModel *> * _Nonnull albumArray) {
            DLog(@"%@", albumArray);
            [weakSelf.dataArray addObjectsFromArray:albumArray];
            [weakSelf.listView reloadData];
        } withCameraImage:^(UIImage * _Nonnull cameraImage) {
            PhotoModel * model = [[PhotoModel alloc] init];
            model.highDefinitionImage = cameraImage;
            [weakSelf.dataArray addObject:model];
            [weakSelf.listView reloadData];
        }];
    }
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (KScreenWidth - 42)/3;
    return CGSizeMake(width, width);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}

-(PubulicTextView*)topTextView{
    if(!_topTextView){
        _topTextView=[PubulicTextView new];
        _topTextView.placeholderText.text = @"请输入问题";
        _topTextView.placeholderText.textColor = UIColorFromRGB(0x959E97);
        _topTextView.placeholderText.font = SYSTEMFONT(14);
        _topTextView.textView.font = BOLDSYSTEMFONT(18);
        _topTextView.textView.textColor=UIColorFromRGB(0x273323);
    }
    return _topTextView;
}
-(PubulicTextView*)bottomTextView{
    if(!_bottomTextView){
        _bottomTextView=[PubulicTextView new];
        _bottomTextView.placeholderText.text = @"添加描述和配图（选填）";
        _bottomTextView.placeholderText.textColor = UIColorFromRGB(0x959E97);
        _bottomTextView.placeholderText.font = SYSTEMFONT(14);
        _bottomTextView.textView.font = BOLDSYSTEMFONT(14);
        _bottomTextView.textView.textColor=UIColorFromRGB(0x454F44);
    }
    return _bottomTextView;
}
-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        _listView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
//        CGRectMake(16, 140, KScreenWidth-32, KScreenHeight-K_TopHeight-36-140)
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = YES;
        [_listView registerClass:[ReleaseImageItem class] forCellWithReuseIdentifier:NSStringFromClass(ReleaseImageItem.class)];
    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc] init];
    }
    return _dataArray;
}

@end
